Data Visualization¶

Step 1: Importing Required Libraries¶

In [1]:
import pandas as pd
import plotly.express as px
from urllib.request import urlopen
import plotly.graph_objects as go
import folium
import json
import gmaps

Step 2: Loading Data into Dataframe¶

In [2]:
df_election = pd.read_csv("election_context-2018.csv")
df_vaccine = pd.read_csv("vaccination_sites_fulton.csv")
df_walmart = pd.read_csv("walmart_store_openings.csv")
print ('Data read into a pandas dataframe!')
Data read into a pandas dataframe!

Step 2(A): Checking Data in Dataframe¶

In [3]:
df_election.head() #First 5 rows of df
Out[3]:
state county fips trump16 clinton16 otherpres16 romney12 obama12 otherpres12 demsen16 ... age29andunder_pct age65andolder_pct median_hh_inc clf_unemploy_pct lesshs_pct lesscollege_pct lesshs_whites_pct lesscollege_whites_pct rural_pct ruralurban_cc
0 Alabama Autauga 1001 18172 5936 865 17379 6363 190 6331.0 ... 40.037058 13.978456 53099.0 5.591657 12.417046 75.407229 10.002112 74.065601 42.002162 2.0
1 Alabama Baldwin 1003 72883 18458 3874 66016 18424 898 19145.0 ... 35.474412 18.714851 51365.0 6.286843 9.972418 70.452889 7.842227 68.405607 42.279099 3.0
2 Alabama Barbour 1005 5454 4871 144 5550 5912 47 4777.0 ... 37.664387 16.528895 33956.0 12.824738 26.235928 87.132213 19.579752 81.364746 67.789635 6.0
3 Alabama Bibb 1007 6738 1874 207 6132 2202 86 2082.0 ... 37.329435 14.885699 39776.0 7.146827 19.301587 88.000000 15.020490 87.471774 68.352607 1.0
4 Alabama Blount 1009 22859 2156 573 20757 2970 279 2980.0 ... 37.240053 17.192916 46212.0 5.953833 19.968585 86.950243 16.643368 86.163610 89.951502 1.0

5 rows × 39 columns

In [4]:
df_vaccine.head() #First 5 rows of df
Out[4]:
Vaccination Site County Address Latitude Longitude
0 1st Care Management Fulton 1120 Hope Rd\nSuite 210\nSandy Springs, GA 30350 33.990280 -84.349400
1 Fulton County Board Of Health (COVID Vaccine S... Fulton 3900 Aviation Circle NW\nAtlanta , GA 30336 33.772980 -84.516279
2 Fulton County Board Of Health (COVID Vaccine S... Fulton Mercedes Benz Stadium\n1 AMB Dr NW\nAtlanta , ... 33.755043 -84.400922
3 Fulton County Board Of Health (COVID Vaccine S... Fulton 4700 North Point Parkway\nAlpharetta, GA 30005 34.055917 -84.268619
4 Fulton County Board Of Health (COVID Vaccine S... Fulton Aviation Community Cultural Center\n3900 Aviat... 33.773032 -84.516298
In [5]:
df_walmart.head() #First 5 rows of df
Out[5]:
storenum OPENDATE date_super conversion st county STREETADDR STRCITY STRSTATE ZIPCODE type_store LAT LON MONTH DAY YEAR
0 1 7/1/62 3/1/97 1.0 5 7 2110 WEST WALNUT Rogers AR 72756 Supercenter 36.342235 -94.07141 7 1 1962
1 2 8/1/64 3/1/96 1.0 5 9 1417 HWY 62/65 N Harrison AR 72601 Supercenter 36.236984 -93.09345 8 1 1964
2 4 8/1/65 3/1/02 1.0 5 7 2901 HWY 412 EAST Siloam Springs AR 72761 Supercenter 36.179905 -94.50208 8 1 1965
3 8 10/1/67 3/1/93 1.0 5 29 1621 NORTH BUSINESS 9 Morrilton AR 72110 Supercenter 35.156491 -92.75858 10 1 1967
4 7 10/1/67 NaN NaN 5 119 3801 CAMP ROBINSON RD. North Little Rock AR 72118 Wal-Mart 34.813269 -92.30229 10 1 1967

Part 1: Create maps with Plotly¶

You must use Plotly Express or Plotly API to create the charts. Do not use Plotly Chart Studio.

  1. Load election_context-2018.csv. Create a choropleth map showing the 2016 county by county senate election results in the state of Georgia. The map should show the map of Georgia and only Georgia. The map should be color coded based on the following requirements.

For each county, compare the columns “demsen16”, “repsen16”, and “othersen16”. If “demsen16” has the highest number, color the county blue in the map. If “repsen16” has the highest number, color the county red in the map. If “othersen16” has the highest number, color the county white in the map. The border of each county should be black.

P1 Step 1: Apply the Conditions for Plotly Charts and assigning it to a new dataframe¶

In [6]:
condition =  df_election['state'] == 'Georgia'
In [7]:
df_georgia = df_election[condition]
In [8]:
df_georgia.head()
Out[8]:
state county fips trump16 clinton16 otherpres16 romney12 obama12 otherpres12 demsen16 ... age29andunder_pct age65andolder_pct median_hh_inc clf_unemploy_pct lesshs_pct lesscollege_pct lesshs_whites_pct lesscollege_whites_pct rural_pct ruralurban_cc
358 Georgia Appling 13001 5494 1434 84 5233 1758 95 1187.0 ... 38.978816 15.524172 37388.0 8.460929 23.624858 88.082103 19.210959 86.158904 71.441106 7.0
359 Georgia Atkinson 13003 1878 697 35 1938 930 34 596.0 ... 43.226899 11.465893 31296.0 5.914870 32.105162 93.321819 22.363919 90.451011 100.000000 9.0
360 Georgia Bacon 13005 3364 608 48 3093 791 47 474.0 ... 41.054129 14.345392 37303.0 5.287009 19.885120 86.802516 16.443529 88.070717 69.286229 7.0
361 Georgia Baker 13007 775 650 15 785 794 10 531.0 ... 33.200000 19.538462 45903.0 3.202847 21.246340 89.000418 14.389652 88.197251 100.000000 3.0
362 Georgia Baldwin 13009 7697 7970 449 7589 8483 166 6787.0 ... 44.974677 13.783619 34595.0 8.202602 18.346188 81.578475 12.792543 73.112521 35.139983 4.0

5 rows × 39 columns

P1 Step 1(A): Filtering Dataframe and creating new column "Winner" for the task¶

In [9]:
df_georgia_cleaned = df_georgia.filter(['fips','county','demsen16','repsen16','othersen16'])
In [10]:
df_georgia_cleaned.head()
Out[10]:
fips county demsen16 repsen16 othersen16
358 13001 Appling 1187.0 5170.0 154.0
359 13003 Atkinson 596.0 1678.0 65.0
360 13005 Bacon 474.0 3046.0 95.0
361 13007 Baker 531.0 763.0 29.0
362 13009 Baldwin 6787.0 7796.0 464.0
In [11]:
df_georgia_cleaned["winner"] = df_georgia_cleaned[["demsen16", "repsen16","othersen16"]].idxmax(axis =1)
In [12]:
df_georgia_cleaned["winner"].value_counts()
Out[12]:
repsen16    138
demsen16     21
Name: winner, dtype: int64

P1 Step 2: Loading Choropleth USA Json File and Creating Choropleth Map with Conditions¶

In [13]:
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)
In [14]:
fig = px.choropleth_mapbox(df_georgia_cleaned, geojson=counties, locations='fips', color= df_georgia_cleaned['winner'],
                           color_discrete_map={'demsen16':'blue', 'repsen16':'red','othersen16':'white'},
                           range_color=(0, 12),
                           mapbox_style="carto-positron",
                           zoom=6, center = {"lat": 32.1656, "lon": -82.9001},
                           opacity=0.5,
                           )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
  1. Load walmart_store_openings.csv and use Plotly to create the following map. Create a Scattergeo map that shows the location of every Walmart store opened since 2000 (including 2000) in the United States. The map must have a title and legend.

The map should show the entire United States. If it’s a “Supercenter”, use a dark blue color to fill the marker. If it’s a “Wal-Mart”, use a light blue color.

P1 Step 3: Creating new dataframe incorporating conditions in task 2¶

In [15]:
condition_walmart =  df_walmart['YEAR'] >= 2000
In [16]:
df_walmart_filtered = df_walmart[condition_walmart]
In [17]:
df_walmart_cleaned = df_walmart_filtered.filter(['storenum','type_store','LAT','LON'])
df_walmart_cleaned['type_store'] = df_walmart_cleaned['type_store'].astype(str)
df_walmart_cleaned.rename(columns = {'type_store':'Type of Store'}, inplace = True) #Renaming Columns

P1 Step 4: Creating Map¶

In [18]:
fig = px.scatter_geo(df_walmart_cleaned, lat="LAT", lon="LON", color="Type of Store",color_discrete_map={'Wal-Mart':'skyblue', 'Supercenter':'darkblue'})
fig.update_traces(marker=dict(size=5))
fig.update_layout(
        title = 'Walmart Stores 2000 and onwards',
        geo_scope='usa',
    )
fig.show()

Part 2 Create maps with Folium¶

  1. Use Folium to create a map visualization showing some Covid vaccination sites. You can use the included spreadsheet vaccination_sites_fulton.csv, or you can create your own list using the Covid vaccination finder at vaccines.gov. Your list must have at least 16 sites.

a. Place markers on a base map (OpenStreetMap). Each marker represents one place based on its latitude and longitude. Each marker should be a circle filled with red color and with a black line.

b. When the mouse cursor hovers over a marker, the place name and street address should be displayed in a tooltip.

In [19]:
df_vaccine.head()
Out[19]:
Vaccination Site County Address Latitude Longitude
0 1st Care Management Fulton 1120 Hope Rd\nSuite 210\nSandy Springs, GA 30350 33.990280 -84.349400
1 Fulton County Board Of Health (COVID Vaccine S... Fulton 3900 Aviation Circle NW\nAtlanta , GA 30336 33.772980 -84.516279
2 Fulton County Board Of Health (COVID Vaccine S... Fulton Mercedes Benz Stadium\n1 AMB Dr NW\nAtlanta , ... 33.755043 -84.400922
3 Fulton County Board Of Health (COVID Vaccine S... Fulton 4700 North Point Parkway\nAlpharetta, GA 30005 34.055917 -84.268619
4 Fulton County Board Of Health (COVID Vaccine S... Fulton Aviation Community Cultural Center\n3900 Aviat... 33.773032 -84.516298

P2 Step 1: Creating Folium Map and Marking Coordinates of Vaccination Center¶

In [20]:
# create an empty map
data_res_map = folium.Map(location=[37.0902,-95.712891], tiles='OpenStreetMap' ,marker_icon='circle') #default_zoom_start=15)

folium.TileLayer('Stamen Terrain').add_to(data_res_map)
folium.TileLayer('Stamen Toner').add_to(data_res_map)
folium.TileLayer('Stamen Water Color').add_to(data_res_map)
folium.TileLayer('cartodbpositron').add_to(data_res_map)
folium.TileLayer('cartodbdark_matter').add_to(data_res_map)
folium.LayerControl().add_to(data_res_map)

# add marker one by one on the map
for i in range(0, len(df_vaccine)):
    
    text = folium.Html('<b>Vaccination Center Name: </b>'+ df_vaccine.iloc[i]['Vaccination Site'] + "<hr style='margin:10px;'>" + 
                       "<ul style='color: #444;list-style-type:circle;align-item:left;padding-left:20px;padding-right:20px'>" +
                       "<li>Address: " + str(df_vaccine.iloc[i]['Address']) + "</li>" +
                       "<li>County: " + str(df_vaccine.iloc[i]['County']) + "</li>", script=True)
    
    popup = folium.Popup(text, max_width=2650)
    #tool = folium.Tooltip(text)
    folium.CircleMarker([ df_vaccine.iloc[i]['Latitude'], df_vaccine.iloc[i]['Longitude'] ], popup=popup,fill_color='red',color = "black").add_to(data_res_map)

P2 Step 1(A): Setting Map Title¶

In [21]:
title_html = '''
             <h3 align="center" style="font-size:20px"><b>Fullton Vaccination Centers</b></h3>
             '''
data_res_map.get_root().html.add_child(folium.Element(title_html))
Out[21]:
<branca.element.Element at 0x205c91006a0>

P2 Step 1(B): Setting Map Bounds and Displaying Map¶

In [22]:
sw = df_vaccine[['Latitude', 'Longitude']].min().values.tolist()
ne = df_vaccine[['Latitude', 'Longitude']].max().values.tolist()

data_res_map.fit_bounds([sw, ne]) 
data_res_map
Out[22]:
Make this Notebook Trusted to load map: File -> Trust Notebook